package controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.*;
import service.AppService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Dell on 2019/1/15.
 */
@Controller
@RequestMapping("/app")
public class AppController {
     @Resource
     AppService appService;

     @RequestMapping("/appList.html")
     public String getAppList(HttpServletRequest request){
         return "dev/list";
     }
    //获取app信息列表
    @RequestMapping("/appinfo")
    @ResponseBody
    public Object appinfo(HttpServletRequest req){

        int currentPageNo = Integer.parseInt(req.getParameter("currentPageNo"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Integer curr = (currentPageNo - 1) * pageSize;
        List<AppInfos> list2 = appService.selectAppInfo(curr, pageSize, 0, 0, null, 0, 0, 0,((DevUser)req.getSession().getAttribute("devuser")).getId());
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("pageSize", pageSize);
        Integer total =appService.selectAppCount(0, 0, null, 0, 0, 0,((DevUser)req.getSession().getAttribute("devuser")).getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list2);
        map.put("curr",curr);
        map.put("limit",pageSize);
        return JSONArray.toJSONString(map);
    }




    @RequestMapping(value="/searchAppInfos",produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Object searchAppInfos(HttpServletRequest req){
        String names=null;
        int states=0;
        int pingTai=0;
        int cate1=0;
        int cate2=0;
        int cate3=0;
        if(req.getParameter("names")!=null||!"".equals(req.getParameter("names"))){
            try {
                names=new String(req.getParameter("names").getBytes("ISO-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if(null!=req.getParameter("states")||!"".equals(req.getParameter("states"))){
            states=Integer.valueOf(req.getParameter("states"));
        }if(null!=req.getParameter("pingTai")||!"".equals(req.getParameter("pingTai"))){
            pingTai=Integer.valueOf(req.getParameter("pingTai"));
        }if(null!=req.getParameter("cate1")||!"".equals(req.getParameter("cate1"))){
            cate1=Integer.valueOf(req.getParameter("cate1"));
        }if(null!=req.getParameter("cate2")||!"".equals(req.getParameter("cate2"))){
            cate2=Integer.valueOf(req.getParameter("cate2"));
        }if(null!=req.getParameter("cate3")||!"".equals(req.getParameter("cate3"))){
            cate3=Integer.valueOf(req.getParameter("cate3"));
        }
        int currentPageNo = Integer.parseInt(req.getParameter("currentPageNo"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Integer curr = (currentPageNo - 1) * pageSize;
        List<AppInfos> list2 = appService.adminSelectAppInfo(curr, pageSize, pingTai, states, names, cate1, cate2, cate3);
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("pageSize", pageSize);
        Integer total =appService.adminSelectAppInfoCount(pingTai, states, names, cate1, cate2, cate3);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list2);
        map.put("curr",curr);
        map.put("limit",pageSize);
        return JSONArray.toJSONString(map);
    }

    @RequestMapping("/look")
    public String appLook(HttpServletRequest req){
        int appid=Integer.valueOf(req.getParameter("id"));
        List<AppVersion> ver=appService.selectAppVersion(appid);
        AppState appstate=appService.selectLook(appid);
        req.getSession().setAttribute("ver", ver);
        req.getSession().setAttribute("appstate", appstate);
        return "dev/look";
    }

    @RequestMapping("/edit")
    public String appEdit(HttpServletRequest req){
        int appid=Integer.valueOf(req.getParameter("id"));
        //List<AppVersion> ver=appService.selectAppVersion(appid);
        AppsState appsState=appService.selectLooks(appid);
        //req.getSession().setAttribute("ver", ver);
        req.getSession().setAttribute("appsState", appsState);
        req.setAttribute("num",appsState.getSv());
        return "dev/editAppOrAdd";
    }

    @RequestMapping("/addAppBase")
    public String btnadds(){
        return "dev/addAppBase";
    }


    @RequestMapping("/role.html")
    public String role(){
        return "admin/role";
    }




    @RequestMapping("/unicode.html")
    public String unicode(){
        return "common/unicode";
    }
    @RequestMapping("/orderList.html")
    public String orderList(HttpServletRequest req){
        if(req.getSession().getAttribute("backuser")==null&&req.getSession().getAttribute("devuser")==null){
            return "404";
        }
        return "orderList";
    }
    @RequestMapping("/sureShenHe")
    public String sureShenHe(HttpServletRequest req){
        int appid=Integer.valueOf(req.getParameter("id"));
        //List<AppVersion> ver=appService.selectAppVersion(appid);
        AppState appstate=appService.selectLook(appid);
        //req.getSession().setAttribute("ver", ver);
        AppVersion appVersion=appService.selectVersion(appid);
        //req.getSession().setAttribute("ver", ver);
        req.setAttribute("appVersion", appVersion);
        req.setAttribute("id", appid);
        req.getSession().setAttribute("appstate", appstate);
        return "admin/shenhe";
    }

    @RequestMapping("/auditingList")
    public String lists(HttpServletRequest req){
        if(req.getSession().getAttribute("backuser")==null&&req.getSession().getAttribute("devuser")==null){
            return "common/404";
        }

        return "admin/auditingList";
    }

    @RequestMapping("/shenHe")
    @ResponseBody
    public Object shenHe(HttpServletRequest req){
        int id=Integer.valueOf(req.getParameter("id"));
        int row=appService.newVersion(id);
        String str="";
        if(row>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }


    @RequestMapping("/updateVersionStatus")
    @ResponseBody
    public Object updateVersionStatus(HttpServletRequest req){

        int id=Integer.valueOf(req.getParameter("appid"));

        int status=Integer.valueOf(req.getParameter("status"));

        String str="";

        int row=appService.updateVersionState(status, id);
        if(row>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }

    @RequestMapping("/index.html")
    public String index(){
        return "index";

    }

    @RequestMapping(value="/searchAppInfo",produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public Object searchAppInfo(HttpServletRequest req){
        String names=null;
        int states=0;
        int pingTai=0;
        int cate1=0;
        int cate2=0;
        int cate3=0;
        if(req.getParameter("names")!=null||!"".equals(req.getParameter("names"))){
            try {
                names=new String(req.getParameter("names").getBytes("ISO-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if(null!=req.getParameter("states")||!"".equals(req.getParameter("states"))){
            states=Integer.valueOf(req.getParameter("states"));
        }if(null!=req.getParameter("pingTai")||!"".equals(req.getParameter("pingTai"))){
            pingTai=Integer.valueOf(req.getParameter("pingTai"));
        }if(null!=req.getParameter("cate1")||!"".equals(req.getParameter("cate1"))){
            cate1=Integer.valueOf(req.getParameter("cate1"));
        }if(null!=req.getParameter("cate2")||!"".equals(req.getParameter("cate2"))){
            cate2=Integer.valueOf(req.getParameter("cate2"));
        }if(null!=req.getParameter("cate3")||!"".equals(req.getParameter("cate3"))){
            cate3=Integer.valueOf(req.getParameter("cate3"));
        }
        int currentPageNo = Integer.parseInt(req.getParameter("currentPageNo"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Integer curr = (currentPageNo - 1) * pageSize;
        List<AppInfos> list2 = appService.selectAppInfo(curr, pageSize, pingTai, states, names, cate1, cate2, cate3,((DevUser)req.getSession().getAttribute("devuser")).getId());
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("pageSize", pageSize);
        Integer total =appService.selectAppCount(pingTai, states, names, cate1, cate2, cate3,((DevUser)req.getSession().getAttribute("devuser")).getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list2);
        map.put("curr",curr);
        map.put("limit",pageSize);
        return JSONArray.toJSONString(map);
    }

    @RequestMapping("/selectCategorys")
    @ResponseBody
    public Object selectCategorys(HttpServletRequest req,@RequestParam(value="parentId")int parentId){
        List<AppCategory> list=appService.selectCategory(parentId);
        return JSONArray.toJSONString(list);

    }

    @RequestMapping(value="/addandupload",produces={"application/json;charset=utf-8;"})
    @ResponseBody
    public Object addandupload(HttpServletRequest req){
        String softwareName=req.getParameter("softwareName");
        String APKName=req.getParameter("APKName");
        String supportROM=req.getParameter("supportROM");
        String interfaceLanguage=req.getParameter("interfaceLanguage");
        double softwareSize=Double.valueOf(req.getParameter("softwareSize"));
        int downloads=Integer.valueOf(req.getParameter("downloads"));
        int cate1=Integer.valueOf(req.getParameter("cate1"));
        int cate2=Integer.valueOf(req.getParameter("cate2"));
        int cate3=Integer.valueOf(req.getParameter("cate3"));
        String desc=req.getParameter("desc");
        int states=Integer.valueOf(req.getParameter("states"));
        int flatformId=Integer.valueOf(req.getParameter("flatformId"));
        //String path=req.getSession().getServletContext().getRealPath("static"+File.separator+"upload");
        //String name="";
//		if(!files.isEmpty()){
//
//			String orderFileName=files.getOriginalFilename();
//			//锟斤拷取源锟侥硷拷锟斤拷
//			String prefix=FilenameUtils.getPrefix(orderFileName);
//			//锟斤拷取锟斤拷缀锟斤拷
//			int fileSize=5000000;
//			if(fileSize>files.getSize()){
//				//锟斤拷锟斤拷锟侥硷拷锟较达拷锟侥达拷小
//				name=orderFileName;
//
//				File upload=new File(path,orderFileName);
//				//锟斤拷锟斤拷File锟斤拷锟斤拷
//				try {
//					files.transferTo(upload);
//					//锟较达拷锟侥硷拷
//				} catch (IllegalStateException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}

        AppInfoss info=new AppInfoss();
        info.setSoftwareName(softwareName);
        info.setAPKName(APKName);
        info.setInterfaceLanguage(interfaceLanguage);
        info.setDevId(((DevUser)req.getSession().getAttribute("devuser")).getId());
        info.setSoftwareSize(softwareSize);
        info.setAppInfo(desc);
        info.setStatus(states);
        info.setCategoryLevel1(cate1);
        info.setCategoryLevel2(cate2);
        info.setCategoryLevel3(cate3);
        info.setDownloads(downloads);
        info.setSupportROM(supportROM);
        info.setFlatformId(flatformId);
        //info.setLogoLocPath(name);
        int row=appService.insertApp(info);
        String str="";
        if(row>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }
    @RequestMapping("/againLoad1")
    @ResponseBody
    public String againLoad1(HttpServletRequest req){

        int id=Integer.valueOf(req.getParameter("id"));

        int num=appService.updateAppStates(4, id);

        String str="";
        if(num>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }
    @RequestMapping("/againLoad2")
    @ResponseBody
    public String againLoad2(HttpServletRequest req){

        int id=Integer.valueOf(req.getParameter("id"));

        int num=appService.updateAppStates(5, id);

        String str="";
        if(num>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(HttpServletRequest req){

        int id=Integer.valueOf(req.getParameter("id"));

        int row=appService.delApp(id);

        String str="";
        if(row>0){
            str="ok";
        }else{
            str="err";
        }
        return JSONArray.toJSONString(str);
    }
}
